﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrafficArchives.Common;
using TrafficArchives.DataAccess;

namespace TrafficArchives.Units
{
    public partial class UnitView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                QueryData();
            }
        }

        private void QueryData()
        {
            var request = new Request { Pagination = { Enabled = true, PageSize = MainPager.PageSize, PageIndex = MainPager.CurrentPageIndex - 1 } };
            if (!string.IsNullOrEmpty(TxtName.Text))
            {
                var con = new Condition
                {
                    FieldName = "DWNAME",
                    Operator = ConditionOperator.Like,
                    Values = new object[] { TxtName.Text }
                };
                request.Items.Add(con);
            }
            if (!string.IsNullOrEmpty(TxtCode.Text))
            {
                var con = new Condition
                {
                    FieldName = "DWGLCODE",
                    Operator = ConditionOperator.Like,
                    Values = new object[] { TxtCode.Text }
                };
                request.Items.Add(con);
            }
            var db = new UnitDb();
            var data = db.GetData(request);
            MainPager.RecordCount = data.RecordCount;
            GridViewMain.DataSource = data.Data;
            GridViewMain.DataBind();

            // 没数据时，显示表头。
            TableHeader.Visible = data.RecordCount == 0;
            UpdatePanelMain.Update(); 
        }

        protected void GridViewMainOnDeleteCommand(object sender, GridViewDeleteEventArgs e)   //触发删除事件
        {
            var dataKey = GridViewMain.DataKeys[e.RowIndex];
            if (dataKey != null)
            {
                string id = dataKey.Value.ToString();
                var db = new UnitDb();
                db.Delete(new List<string>{id});
                QueryData();
            }
        }

        protected void GridViewMainOnUpdateCommand(object sender, GridViewUpdateEventArgs e)   //触发删除事件
        {
            var dataKey = GridViewMain.DataKeys[e.RowIndex];
            if (dataKey != null)
            {
                string id = dataKey.Value.ToString();
                Response.Redirect(string.Format("AddUnit.aspx?action=edit&id={0}",id));
            }
        }


        protected void BtnAddClick(object sender, EventArgs e)
        {
            Response.Redirect("AddUnit.aspx");
        }

        protected void MainPagerPageIndexChanged(object sender, EventArgs e)
        {
            QueryData();
        }

        protected void BtnQueryClicked(object sender, EventArgs e)
        {
            MainPager.CurrentPageIndex = 1;

            QueryData();
        }
    }
}